using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using TTDotNet.Engine.Exceptions;
using TTDotNet.Util;
using TTDotNet.Engine.ThreeDEngine;
using TTDotNet.Engine.Stations;
using TTDotNet.Engine.Stations.Depot;
using TTDotNet.Engine.GUI.Windows.Info;
using TTDotNet.Engine.GUI.Windows.Depot;

namespace TTDotNet.Engine.Create.Tools
{
    public class StationSelectTool : Tool
    {
        private Station lastSelected;
        private bool openWindow;
        
        public StationSelectTool(Game game)
            : base(game, ToolType.StationSelect)
        {

        }

        public override void MouseDown(Point mouseLocation, GridPickData data)
        {
            Coordinate selected = data.Coord.Clone();

            try
            {
                lastSelected = Game.gameWorld.Map.GetStationOnTile(selected.X, selected.Z);
            }
            catch (CoordinateOutOfBoundsException e)
            {
                lastSelected = null;
            }

            if (openWindow && lastSelected != null)
            {
                if (lastSelected is Depot)
                {
                    if (lastSelected is RoadDepot)
                    {
                        RoadDepotWindow depotWindow = new RoadDepotWindow(new PointF(200, 200), lastSelected as RoadDepot);
                        Game.newGUI.OpenNewWindow(depotWindow);
                    }
                }
                else
                {
                    StationInfoWindow infoWindow = new StationInfoWindow(new PointF(200, 200), lastSelected);
                    Game.newGUI.OpenNewWindow(infoWindow);
                }

                lastSelected = null;
            }

            
        }

        public bool OpenWindows
        {
            get { return openWindow; }
            set { openWindow = value; }
        }

        public Station Selected
        {
            get { return lastSelected; }
        }

        public void Reset()
        {
            lastSelected = null;
        }

    }
}
